import get_menus from '@dfeidao/atom-nodejs/authorization/get-menus';
import { IFeidaoAiNodejsComponent } from '@dfeidao/atom-nodejs/interfaces';
import render from '@dfeidao/atom-nodejs/render/render';
import { productid } from '../../atom/config';
import P01 from './p01';

export default async function na001(fd: IFeidaoAiNodejsComponent) {
	try {
		const menu = await get_menus(fd.data.msg, fd.data.headers, productid, '3');
		const page_name = fd.data.url.match(/.*\/((.*?)\.html)/)[1];
		const menuArry = menu.reduce((p, c) => {
			if (!c.parent_no) {
				p.set(c.menu_no, { ...c, child: new Array(), page_name, is_open: c.package_url === page_name ? true : false });
			}
			return p;
		}, new Map());
		menu.forEach((p) => {
			if (menuArry.get(p.parent_no)) {
				if (p.package_url === page_name) {
					menuArry.get(p.parent_no).is_open = true;
				}
				menuArry.get(p.parent_no).child.push({ ...p, is_open: p.package_url === page_name ? true : false });
			}
		});
		const menus = Array.from(menuArry.values());
		render(fd.data.node, menus, P01, 'P01');
	} catch (error) {
		throw new Error(error.message);
	}
}
